import {Action, getModule, Module, Mutation, VuexModule} from "vuex-module-decorators";
import store from "@/store";
import {Product} from "@/model/Product";
import axios from "axios";

@Module({
    name: "productStore",
    dynamic: true,
    store,
    namespaced:true
})
class ProductStore extends VuexModule {
    public productItems: Array<Product> = [];

    @Mutation
    public UPDATE_PRODUCT_ITEMS(productItems: Array<Product>) {
        this.productItems = productItems;
    }

    @Action
    public getProductItems() {
        axios.get("/api/product/getProducts", {}).then(({data}) => {
            const products: Array<Product> = data as Array<Product>;
            this.context.commit("UPDATE_PRODUCT_ITEMS",products);
        }).catch((error) => {
            console.log(error);
        })
    }

    get getAllProducts() {
        return this.productItems;
    }
}

export const productStore = getModule(ProductStore);
